package com.qdlc.p2p.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qdlc.p2p.biz.executer.AbstractExecuter;
import com.qdlc.p2p.biz.executer.ExecuterHelper;
import com.qdlc.p2p.biz.executer.impl.SetBonusExecuter;
import com.qdlc.p2p.biz.service.TppBonusDetailsService;
import com.qdlc.p2p.biz.tpp.TppBaseInterface;
import com.qdlc.p2p.biz.tpp.TppSinaServiceImpl;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.TppBonusDetailsDao;
import com.qdlc.p2p.dal.dto.TppBonusDetails;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.TppAccountModel;
import com.qdlc.p2p.dal.model.TppModel;
import com.qdlc.p2p.dal.util.SystemConfigs;

/**
 * 新浪存钱罐收益业务处理
 *
 * @author ygw
 * @version 2.0
 * @date 2015年11月18日 下午4:26:08
 */
@Service("tppBonusDetailsService")
@Transactional
public class TppBonusDetailsServiceImpl implements TppBonusDetailsService {
    private static Logger logger = Logger.getLogger(TppBonusDetailsServiceImpl.class);

    @Resource
    private TppBonusDetailsDao tppBonusDetailsDao;

    @Override
    public void queryBonusDetails(User user) {
        TppModel detailsTppModel = new TppModel();
        // 查询第三方托管账户收支明细(前2天到当天)
        detailsTppModel.setUser(user);
        detailsTppModel.setStartTime(DateUtil.dateStr7(DateUtil.rollDay(DateUtil.getNow(), -2)) + "000000");
        detailsTppModel.setEndTime(DateUtil.dateStr7(DateUtil.getNow()) + "235959");
        detailsTppModel.setExtendParam("svp_trade_type^bonus"); // svp_trade_type^bonus – 只返回收益;svp_trade_type^in_out – 只返回收入和支出
        TppBaseInterface detailsTppService = new TppSinaServiceImpl();

        TppAccountModel accountDetails = (TppAccountModel) detailsTppService.tppQueryAccountDetails(detailsTppModel);
        if (null == accountDetails) {
            // 第三方托管账户收支明细未找到！
            logger.error("用户：" + user.getTppUserCustId() + "第三方托管账户收支明细未找到！");
            return;
        }
        if (!StringUtil.isBlank(accountDetails.getDetail_list())) {
            String[] detailList = accountDetails.getDetail_list().split("\\|");
            for (String dl : detailList) {
                String details[] = dl.split("\\^");
                if (details.length > 5) {
                    String bd[] = details[0].split("-");
                    String d = DateUtil.dateStr10(DateUtil.getNow()) + bd[0];
                    // 查询存钱罐收益明细表
                    QueryParam param = QueryParam.getInstance();
                    param.addParam("user", user).addParam("bonusDate", d);
                    List<TppBonusDetails> bonusDetails = tppBonusDetailsDao.findByCriteria(param);
                    double bonus = Double.parseDouble(details[3]);// 收益
                    if (bonusDetails.size() <= 0 && bonus != 0) {// 已经插入的场合，收益=0，不再插入
                        // 更新到存钱罐收益明细表
                        TppBonusDetails tppBonusDetails = new TppBonusDetails(user);
                        tppBonusDetails.setBonusDate(DateUtil.dateStr10(DateUtil.getNow()) + bd[0]);// 收益日期
                        tppBonusDetails.setBonus(bonus);// 收益
                        tppBonusDetails.setBalance(Double.parseDouble(details[4]));// 余额
                        tppBonusDetails.setAddTime(DateUtil.getNow());// 添加时间
                        tppBonusDetailsDao.save(tppBonusDetails);
                    }
                }
            }
        }
    }

    @Override
    public void setBonusDetails(TppBonusDetails bonusDetail) {
        // 存钱罐收益更新到平台可用金额
        Global.setTransfer("money", bonusDetail.getBonus());
        AbstractExecuter setBonusExecuter =  ExecuterHelper.getExecuter(SetBonusExecuter.class);
        setBonusExecuter.execute(bonusDetail.getBonus(), bonusDetail.getUser(), new User(SystemConfigs.ADMIN_ID));

        // 更新状态为已处理
        bonusDetail.setStatus(1);
        bonusDetail.setUpdateTime(DateUtil.getNow());
        tppBonusDetailsDao.update(bonusDetail);
    }
}
